package com.mxl.dao.cashorder.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.mxl.common.helper.CardType;
import com.mxl.dao.cashorder.model.ChargeChannel;
import com.mxl.datebase.orm.hibernate.BaseDaoSupport;
import com.mxl.service.cashorder.dto.SelectChannelDto;

public class ChargeChannelDaoImpl extends BaseDaoSupport<ChargeChannel, Long> {

	public List<ChargeChannel> queryAvailableChannel(SelectChannelDto dto) {
		List<Criterion> criterions = new ArrayList<Criterion>();
		criterions.add(Restrictions.eq("status", 1));
		criterions.add(Restrictions.eq("bossType", dto.getBossType()));
		criterions.add(Restrictions.eq("chargeType", dto.getCardType()));
		criterions.add(Restrictions.sqlRestriction("instr(amounts, ?) > 0",
				dto.getSelectAmount(), Hibernate.STRING));
		if (CardType.QUANGUO_CARD.getValue() == dto.getCardType()) {
			Object[] supportCardType = { 0, 2 };
			criterions.add(Restrictions.in("supportCardType", supportCardType));
		} else {
			criterions.add(Restrictions.sqlRestriction(
					"instr(cardRegion, ?) > 0", dto.getCardRegion(),
					Hibernate.STRING));
			Object[] supportCardType = { 1, 2 };
			criterions.add(Restrictions.in("supportCardType", supportCardType));
		}
		return this.find(new Order[]{Order.asc("assignRate")},criterions.toArray(new Criterion[criterions.size()]));
	}
}
